package com.controller;

import com.pojo.Result;
import com.service.NoteProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/note-products")
public class NoteProductsController {

    @Autowired
    private NoteProductsService noteProductsService;

    @PostMapping
    public Result<Void> addProductToNote(
            @RequestParam Integer noteId,
            @RequestParam Integer productId) {
        return noteProductsService.addProductToNote(noteId, productId);
    }

    @DeleteMapping
    public Result<Void> removeProductFromNote(
            @RequestParam Integer noteId,
            @RequestParam Integer productId) {
        return noteProductsService.removeProductFromNote(noteId, productId);
    }

    @DeleteMapping("/note/{noteId}")
    public Result<Void> removeAllProductsFromNote(@PathVariable Integer noteId) {
        return noteProductsService.removeAllProductsFromNote(noteId);
    }

    @DeleteMapping("/product/{productId}")
    public Result<Void> removeProductFromAllNotes(@PathVariable Integer productId) {
        return noteProductsService.removeProductFromAllNotes(productId);
    }

    @GetMapping("/note/{noteId}/products")
    public Result<List<Integer>> getProductsByNoteId(@PathVariable Integer noteId) {
        return noteProductsService.getProductsByNoteId(noteId);
    }

    @GetMapping("/product/{productId}/notes")
    public Result<List<Integer>> getNotesByProductId(@PathVariable Integer productId) {
        return noteProductsService.getNotesByProductId(productId);
    }

    @GetMapping("/exists")
    public Result<Boolean> isProductAttachedToNote(
            @RequestParam Integer noteId,
            @RequestParam Integer productId) {
        return noteProductsService.isProductAttachedToNote(noteId, productId);
    }
}